﻿#region << 文件说明 >>

/*----------------------------------------------------------------
 * 命名空间：Vampirewal.BlogCore.Controls.Container
 * 唯一标识：686b33d2-7d53-4f3d-bb8f-4dc3a5a08c68
 * 文件名：Collapse
 *
 * 创建者：杨程
 * 电子邮箱：235160615@qq.com
 * 创建时间：2024/6/27 16:35:35
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/

#endregion << 文件说明 >>

namespace Vampirewal.BlogCore;

/// <summary>
/// 折叠面板容器控件
/// </summary>
public sealed class Collapse : ContainerControlBase<CollapseItem>
{
    /// <summary>
    /// 构造函数
    /// </summary>
    internal Collapse()
    {
    }

    public override List<CollapseItem> Children { get; internal set; }
    public override string Id { get; set; }
    public override string Title { get; set; }

    public override string ConverToHtml()
    {
        StringBuilder sb = new StringBuilder();

        sb.AppendLine($"<el-collapse accordion>");
        foreach (var item in Children)
        {
            sb.AppendLine(item.ConverToHtml());
        }
        sb.AppendLine($"</el-collapse>");

        return sb.ToString();
    }
}

public static partial class App
{
    /// <summary>
    /// 创建折叠面板控件
    /// </summary>
    /// <param name="children"></param>
    /// <returns></returns>
    public static Collapse CreateCollapse(Action<List<CollapseItem>> children)
    {
        Collapse collapse = new Collapse()
        {
            Children = new List<CollapseItem>()
        };

        children?.Invoke(collapse.Children);
        return collapse;
    }

    /// <summary>
    /// 将折叠面板单项添加进集合
    /// </summary>
    /// <param name="chilren"></param>
    /// <param name="control"></param>
    public static void AddToChildren(this CollapseItem chilren, List<CollapseItem> control)
    {
        chilren.Id = $"{control.Count}";
        control.Add(chilren);
    }
}

/// <summary>
/// 折叠面板单项控件
/// </summary>
public sealed class CollapseItem : ContainerControlBaseSingle
{
    internal CollapseItem()
    {
    }

    public override string Id { get; set; }
    public override string Title { get; set; }
    public override ControlBase Control { get; set; }

    public override string ConverToHtml()
    {
        StringBuilder sb = new StringBuilder();

        sb.AppendLine($"<el-collapse-item title=\"{Title}\" name=\"{Id}\">");
        sb.AppendLine(Control.ConverToHtml());
        sb.AppendLine($"</el-collapse-item>");

        return sb.ToString();
    }
}

public static partial class App
{
    /// <summary>
    /// 创建折叠面板单项控件
    /// </summary>
    /// <param name="title"></param>
    /// <param name="control"></param>
    /// <returns></returns>
    public static CollapseItem CreateCollapseItem(string title, Func<ControlBase> control)
    {
        CollapseItem item = new CollapseItem()
        {
            Title = title,
        };

        item.Control = control?.Invoke();

        if (item.Control == null)
            throw new Exception("未创建子控件实例！");

        return item;
    }
}